<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="商品名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button-group>
                    <el-button type="primary">查询</el-button>
                    <el-button type="warning" @click="handleAdd">新增</el-button>
                    </el-button-group>
                </el-form-item>
                <el-form-item>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="admins" highlight-current-row v-loading="listLoading" style="width: 100%;">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" width="60" label="ID" sortable></el-table-column>
            <el-table-column property="avatar" label="头像" align="center">
                <template slot-scope="scope">
                    <el-image
                        style="width: 50px; height: auto;"
                        :src="scope.row.avatar"
                        :preview-src-list="[scope.row.avatar]">
                        >
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column prop="user_name" label="管理员"></el-table-column>
            <el-table-column prop="account" label="账户" sortable></el-table-column>
            <el-table-column prop="created_at" label="创建时间"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button-group>
                        <el-button type="warning" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="幻灯片名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="addForm.sex">
                        <el-radio class="radio" :label="1">男</el-radio>
                        <el-radio class="radio" :label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="链接">
                    <el-input-number v-model="addForm.link"></el-input-number>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input type="textarea" v-model="addForm.addr"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--编辑界面-->
        <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
                <el-form-item label="幻灯片名称" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="幻灯片">
                    <div class="originImg">
                        <img width="auto" height="100" class="el-upload-list__item-thumbnail" :src="c_image.url" alt="">
                    </div>
                    <el-upload
                        name="file"
                        action="#"
                        list-type="picture-card"
                        :limit="1"
                        :auto-upload="false">
                        <i slot="default" class="el-icon-plus"></i>
                        <div slot="file" slot-scope="{file}">
                            <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
                            <span class="el-upload-list__item-actions">
                                <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                                <i class="el-icon-zoom-in"></i>
                                </span>
                                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                                    <i class="el-icon-delete"></i>
                                </span>
                            </span>
                        </div>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-form-item label="链接">
                    <el-input v-model="editForm.link"></el-input>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input type="textarea" v-model="editForm.description"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    import { getAdminList } from "../../api/api";
    export default {
        data() {
            return {
                filters:{
                    name: ''
                },
                c_image:{},
                dialogImageUrl: '',
                dialogVisible: false,
                disabled: false,
                admins: [],
                total: 0,
                per_page: 0,
                page: 1,
                listLoading: false,
                sels: [],//列表选中列
                editFormVisible: false,  //编辑界面是否显示
                addFormVisible: false,  //新增界面数据
                addLoading: false,
                addForm: {
                    name: '',
                    sex: -1,
                    age: 0,
                    birth: '',
                    addr: ''
                },
                addFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
                editLoading: false,
                editFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                editForm: {
                    name: '',
                    image_url: '',
                    description: '',
                    link: '',
                }
            }
        },
        methods: {
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    sex: -1,
                    age: 0,
                    birth: '',
                    addr: ''
                };
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);
                            para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            addUser(para).then((res) => {
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.get_banners();
                            });
                        });
                    }
                });
            },
            handleRemove(file) {
                console.log(file);
            },
            handlePictureCardPreview(file) {
                console.log(file)
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleCurrentChange(val) {
                this.page = val;
                this.get_admin();
            },
            //获取管理员列表
            get_admin() {
                let para = {
                    page: this.page,
                };
                var this_ = this;
                this_.listLoading = true;
                getAdminList(para).then((res) => {
                    this_.total = res.data.data.total;
                    this_.per_page = parseInt(res.data.data.per_page);
                    this_.admins = res.data.data.data;
                    this_.listLoading = false;
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = { product_id: row.id };
                    removeProduct(para).then((res) => {
                        this.listLoading = false;
                        console.log(res);
                        if (res.code == 200){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.get_product();
                        }else {
                            this.$message({
                                message: res.msg,
                                type: 'fail'
                            });
                        }

                    });
                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
                console.log(row)
                this.c_image = {'name': row.name, 'url': row.image_url}
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm);
                            editBanner(para).then((res) => {
                                this.editLoading = false;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.get_product();
                            });
                        });
                    }
                });
            }
        },
        mounted() {
            this.get_admin();
        }
    }

</script>

<style scoped>
    .originImg{
        width: 100%;
    }
    .el-button{
        margin-left: 0 !important;
    }
</style>
